package tester.polymer;

import tester.Utils.PubUtil;
import tester.component.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Tester {
    public static void main(String[] args) {
        Tester tester = new Tester();
        //tester.maker("./src/main/resources/data/process.txt");
        tester.tester("./src/main/resources/data/process.txt");
    }
    public void maker(String filePath) {
        List<String> lines = reader(filePath);
        Configer.config(lines);
        for (String line : lines) {
            String prefixStr = PubUtil.getPrefix(line);
            String suffixStr = PubUtil.getSuffix(line);
            if (prefixStr == null) continue;
            String[] prefixs = prefixStr.split("\\.");
            if (prefixs[0].equals(Configer.prefix)) continue;


            switch (prefixs[0]) {
                case Sender.prefix:
                    Sender.send(prefixs, suffixStr);
                    break;
                case Saver.prefix:
                    Saver.save(suffixStr);
                    break;
                case Loader.prefix:
                    Loader.load(suffixStr);
                    break;
                default:
                    break;
            }
        }
    }
    public void tester(String filePath) {
        List<String> lines = reader(filePath);
        Configer.config(lines);
        for (String line : lines) {
            String prefixStr = PubUtil.getPrefix(line);
            String suffixStr = PubUtil.getSuffix(line);
            if (prefixStr == null) continue;
            String[] prefixs = prefixStr.split("\\.");
            if (prefixs[0].equals(Configer.prefix)) continue;


            switch (prefixs[0]) {
                case Sender.prefix:
                    Sender.send(prefixs, suffixStr);
                    break;
                case Comparator.prefix:
                    Saver.save(suffixStr+"back");
                    Comparator.compare(suffixStr,suffixStr+"back");
                    PubUtil.deleteFile(Saver.getFilePath(suffixStr+"back"));
                    break;
                case Loader.prefix:
                    Loader.load(suffixStr);
                    break;
                default:
                    break;
            }
        }
    }
    public List<String> reader(String filePath) {
        List<String> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line.trim());
                System.out.println(line);
                // 处理每一行的内容
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
